package com.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.entity.SmbmsRole;
import com.wx.entity.SmbmsUser;
import com.wx.mapper.SmbmsRoleMapper;
import com.wx.mapper.SmbmsUserMapper;
import com.wx.service.SmbmsUserService;
import com.wx.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SmbmsUserServiceImpl implements SmbmsUserService {

    @Autowired
    SmbmsUserMapper smbmsUserMapper;

    @Override
    public void addSmbmsUser(SmbmsUser smbmsUser) {
        smbmsUserMapper.insert(smbmsUser);
    }

    @Override
    public void delSmbmsUser(String id) {
        smbmsUserMapper.deleteById(id);
    }

    @Override
    public SmbmsUser getSmbmsUserById(String id) {
        SmbmsUser smbmsUser = smbmsUserMapper.selectById(id);
        SmbmsRole smbmsRole = smbmsRoleMapper.selectById(smbmsUser.getUserRole());
        smbmsUser.setUserRoleName(smbmsRole.getRoleName());
        return smbmsUser;
    }

    @Override
    public void updSmbmsUser(SmbmsUser smbmsUser) {
        smbmsUserMapper.updateById(smbmsUser);
    }

    @Autowired
    SmbmsRoleMapper smbmsRoleMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public IPage<SmbmsUser> getSmbmsUserByPage(SmbmsUser smbmsUser) {
        Object userListObj = redisTemplate.opsForValue().get("userList");
        if (userListObj == null) {
            IPage<SmbmsUser> page = new Page<>();
            page.setCurrent(smbmsUser.getCurrentPage());
            page.setSize(10);
            LambdaQueryWrapper<SmbmsUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(smbmsUser.getUserName())) {
                lambdaQueryWrapper.like(SmbmsUser::getUserName, smbmsUser.getUserName());
            }
            if (smbmsUser.getUserRole() != 0) {
                lambdaQueryWrapper.eq(SmbmsUser::getUserRole, smbmsUser.getUserRole());
            }
            IPage<SmbmsUser> smbmsUserIPage = smbmsUserMapper.selectPage(page, lambdaQueryWrapper);
            for (SmbmsUser smbmsUser1 : smbmsUserIPage.getRecords()) {
                SmbmsRole smbmsRole = smbmsRoleMapper.selectById(smbmsUser1.getUserRole());
                smbmsUser1.setUserRoleName(smbmsRole.getRoleName());
//             new Date().getYear();
//             smbmsUser1.getBirthday().getYear();
                String nowYear = DateUtils.format(new Date(), DateUtils.DATE_YEAE);
                String birthday = DateUtils.format(smbmsUser1.getBirthday(), DateUtils.DATE_YEAE);
                int age = Integer.valueOf(nowYear) - Integer.valueOf(birthday);
                smbmsUser1.setAge(age);
            }
            redisTemplate.opsForValue().set("userList",smbmsUserIPage);
            return smbmsUserIPage;
        }else {
            return (IPage<SmbmsUser>)userListObj;
        }

//        smbmsUserIPage.getRecords().forEach(smbmsUser1 -> {
//            SmbmsRole smbmsRole =  smbmsRoleMapper.selectById(smbmsUser1.getUserRole());
//            smbmsUser1.setUserRoleName(smbmsRole.getRoleName());
////             new Date().getYear();
////             smbmsUser1.getBirthday().getYear();
//            String nowYear  = DateUtils.format(new Date(),DateUtils.DATE_YEAE);
//            String birthday = DateUtils.format(smbmsUser1.getBirthday(),DateUtils.DATE_YEAE);
//            int age = Integer.valueOf(nowYear) -Integer.valueOf(birthday);
//            smbmsUser1.setAge(age);
//        });

    }

    @Override
    public SmbmsUser getSmbmsUserByUserCode(String userCode) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        //                     数据库字段名 硬编码
//        queryWrapper.eq("user_code",userCode);
        LambdaQueryWrapper<SmbmsUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SmbmsUser::getUserCode, userCode);
        List<SmbmsUser> selectList = smbmsUserMapper.selectList(lambdaQueryWrapper);
        if (selectList.size() == 0) {
            return null;
        } else {
            return selectList.get(0);
        }
    }
}
